package com.stylefeng.guns.rest.modular.App.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.rest.modular.App.dao.TaskReleaseMapper;
import com.stylefeng.guns.rest.modular.App.model.TaskRelease;
import com.stylefeng.guns.rest.modular.App.service.ITaskReleaseService;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 抢单释放记录表 Service 实现 
 * @author 张亚林
 * @Date 2018-11-12 17:46
 */
@Service
public class TaskReleaseServiceImpl extends ServiceImpl<TaskReleaseMapper,TaskRelease> implements ITaskReleaseService {

    @Resource
    private TaskReleaseMapper taskReleaseMapper;
    /**
     * 查询抢单释放记录表列表
     * @param taskRelease 抢单释放记录表对象
     * @return
     */
    public List<TaskRelease> selectListTaskRelease(TaskRelease taskRelease,String token)throws Exception{
        return  this.taskReleaseMapper.selectListTaskRelease(taskRelease);
    }
    /**
	 * 分页查询抢单释放记录表列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListTaskRelease(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(taskReleaseMapper.selectListTaskRelease(page,(TaskRelease)pager.getObj()));
        List<TaskRelease> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param taskRelease 抢单释放记录表对象
     * @return 
     */
    public  List<TaskRelease> selectListTaskReleaseForUpdate(TaskRelease taskRelease)throws Exception{
        return  this.taskReleaseMapper.selectListTaskReleaseForUpdate(taskRelease);
    }
    /**
     * 通过主键id查询的抢单释放记录表列表
     * @param id 主键id
     * @return     
     */
    public  TaskRelease findTaskReleaseById(String id,String token)throws Exception{
        return  this.taskReleaseMapper.findTaskReleaseById(id);
    }

    /**
     * 新增抢单释放记录表数据
     * @param taskRelease  抢单释放记录表对象
     * @return
     */
    @Transactional
    public int insertTaskRelease(TaskRelease taskRelease,String token) throws Exception { 
        if(taskRelease!=null && (taskRelease.getTrId()==null || taskRelease.getTrId().trim().length()<10)){
            taskRelease.setTrId(IdWorker.get32UUID());
        }  
        return this.taskReleaseMapper.insertTaskRelease(taskRelease);
    }
    
    /**
     * 更新抢单释放记录表数据
     * @param taskRelease 抢单释放记录表对象
     * @return     
     */
    @Transactional
    public int updateTaskRelease(TaskRelease taskRelease,String token) throws Exception {     	
        return  this.taskReleaseMapper.updateTaskRelease(taskRelease);
    }

    @Override
    public int updateTaskReleaseRefuse(TaskRelease taskRelease, String token) throws Exception {
        return this.taskReleaseMapper.updateTaskReleaseRefuse(taskRelease);
    }

    /**
     * 查询抢单释放记录表返回key-Value属性
     * @param taskRelease 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsTaskRelease(TaskRelease taskRelease,String token) throws Exception {
        List<TaskRelease> listTaskRelease=this.taskReleaseMapper.selectListTaskRelease(taskRelease);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(TaskRelease item:listTaskRelease){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(item.getTrId());
            opts.setValue(item.getTiId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
